import { createStore } from 'redux'

let reducer = (state, { type, list, index, value, obj}) => {
    switch (type) {
        case "ADD_ITEM":
            state.list.push(list)
            return state
        case "DEL_ITEM":
            state.list.splice(index, 1)
            return state
        case "SEARCH":
            state.list = state.list.filter(item => item.name.includes(value))
            return state
        case "CHANGE_ITEM":
            state.list = state.list.map(item => item.key === obj.key? obj: item)
            return state
        default:
            return state
    }
}

let initState = {
    list: [
        {
            key: '1',
            name: '前端',
            short: "End",
            num: 10
        },
        {
            key: '2',
            name: '后端',
            short: "Back",
            num: 20
        },
        {
            key: '3',
            name: '测试',
            short: "Test",
            num: 16
        },
        {
            key: '4',
            name: '运维',
            short: "Ops",
            num: 26
        }
    ]
}

let store = createStore(reducer, initState)
window.store = store

export default store